package com.sky.controller.user;

import com.alibaba.druid.support.json.JSONUtils;
import com.sky.dto.*;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.OrderService;
import com.sky.vo.OrderPaymentVO;
import com.sky.vo.OrderSearchVO;
import com.sky.vo.OrderSubmitVO;
import com.sky.vo.OrderVO;
import com.sky.websocket.WebSocketServer;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController("userOrderController")
@RequestMapping("/user/order")
@Slf4j
public class OrderController {
    @Autowired
    private OrderService orderService;
    @Autowired
    private WebSocketServer webSocketServer;

    @RequestMapping("/submit")
    public Result<OrderSubmitVO> submit(@RequestBody OrdersSubmitDTO ordersSubmitDTO){
        log.info("用户下单：{}", ordersSubmitDTO);
        OrderSubmitVO orderSubmitVO = orderService.submit(ordersSubmitDTO);
        return Result.success(orderSubmitVO);
    }
    @GetMapping("/orderDetail/{id}")
    public Result<OrderSearchVO> getById(@PathVariable("id") Long id){
        log.info("查询订单详情：{}", id);
        OrderSearchVO orderSearchVO = orderService.getById(id);
        return Result.success(orderSearchVO);
    }
    @GetMapping("/historyOrders")
    public Result<PageResult> conditionSearch(OrdersPageQueryDTO ordersPageQueryDTO){
        log.info("分页查询订单：{}", ordersPageQueryDTO);
        PageResult pageResult = orderService.conditionSearchWithUserId(ordersPageQueryDTO);
        return Result.success(pageResult);
    }
    @PostMapping("/repetition/{id}")
    public Result repetion(@PathVariable("id") Long id){
        log.info("再来一单：{}", id);
        orderService.repetion(id);
        return Result.success();
    }
    @PutMapping("/cancel/{id}")
    public Result cancel(@PathVariable("id") Long id){
        log.info("取消订单：{}", id);
        OrdersCancelDTO ordersCancelDTO = new OrdersCancelDTO();
        ordersCancelDTO.setId(id);
        ordersCancelDTO.setCancelReason("用户取消");
        orderService.cancel(ordersCancelDTO);
        return Result.success();
    }
    /**
     * 订单支付
     *
     * @param ordersPaymentDTO
     * @return
     */
    @PutMapping("/payment")
    @ApiOperation("订单支付")
    public Result<OrderPaymentVO> payment(@RequestBody OrdersPaymentDTO ordersPaymentDTO) throws Exception {
        log.info("订单支付：{}", ordersPaymentDTO);
        //OrderPaymentVO orderPaymentVO = orderService.payment(ordersPaymentDTO);
        //跳过支付模块 直接进行接单提醒
        orderService.paySuccess(ordersPaymentDTO.getOrderNumber());
        Map map = new HashMap();
        map.put("type", "1");
        map.put("orderId", ordersPaymentDTO.getOrderNumber());
        map.put("content", "订单号"+ordersPaymentDTO.getOrderNumber());
        webSocketServer.sendToAllClient(JSONUtils.toJSONString(map));
        OrderPaymentVO orderPaymentVO = new OrderPaymentVO();
        log.info("生成预支付交易单：{}", orderPaymentVO);
        return Result.success(orderPaymentVO);
    }
    @GetMapping("/reminder/{id}")
    public Result reminder(@PathVariable("id") Long id){
        OrderSearchVO orderSearchVO = orderService.getById(id);
        String orderNumber = orderSearchVO.getNumber();
        Map map = new HashMap();
        map.put("type", "2");
        map.put("orderId", orderNumber);
        map.put("content", "订单号"+orderNumber);
        webSocketServer.sendToAllClient(JSONUtils.toJSONString(map));
        return Result.success();
    }
}
